import re
import sys

class FindFile:
	def __init__(self, file_name):
		self.file_name = file_name
		self.afile = open(file_name, 'r')
		self.all_lines = self.afile.readlines()
		self.afile.close()

	def find_key(self, key, case_sensitive=True):
		if case_sensitive:
			return [i for i, line in enumerate(self.all_lines) if key in line]
		else:
			return [i for i, line in enumerate(self.all_lines) if key.lower() in line.lower()]

	def find_BunderA(self, A, B, case_sensitive=True):
		re_list = []
		flags = 0 if case_sensitive else re.IGNORECASE
		pattern_A = re.compile(A, flags)
		pattern_B = re.compile(B, flags)

		for i, line in enumerate(self.all_lines):
			if pattern_A.search(line):
				for j in range(i, len(self.all_lines)):
					if pattern_B.search(self.all_lines[j]):
						re_list.append((i, j))
						break
		return re_list

class FindPin:
	def __init__(self, afile):
		self.ff = FindFile(afile)
		self.all_lines = self.ff.all_lines

	def get_lef_pin(self):
		key = ["PIN", "DIRECTION"]
		re_list = []
		re_index = self.ff.find_BunderA(key[0], key[1])
		for i, turple in enumerate(re_index):
			line_1 = self.all_lines[turple[0]]
			r1 = line_1[6:-1]
			line_2 = self.all_lines[turple[1]]
			r2 = line_2[14:-3]
			re_list.append((r1, r2))		
		return sorted(re_list, key=lambda x:x[0])

	def get_lib_pin(self):
		key = ["^pin", "^pg_pin", "direction", "direction"]
		re_list = []
		re_index = self.ff.find_BunderA(key[0], key[2])
		for i, turple in enumerate(re_index):
			line_1 = self.all_lines[turple[0]]
			r1 = line_1[5:-5]
			line_2 = self.all_lines[turple[1]]
			r2 = line_2[13:-3]
			re_list.append((r1, r2))

		re_index = self.ff.find_BunderA(key[1], key[3])
		for i, turple in enumerate(re_index):
			line_1 = self.all_lines[turple[0]]
			r1 = line_1[8:-5]
			line_2 = self.all_lines[turple[1]]
			r2 = line_2[13:-2]
			re_list.append((r1, r2))

		return sorted(re_list, key=lambda x:x[0])
		
lib = sys.argv[1]
name = sys.argv[2]


if lib == "NDPHY":
	path_lib = "/designs/cascade/ip/ig/NDPHY_SPEEDPLUS_FC/rev2.3.1/lib/"+name+"_tt_0p75v_25c_typical.lib"
elif lib == "DDRPHY":
	path_lib = "/designs/cascade/ip/ig/DDRPHY/rev2.8.0/lib/"+name+"_tt_0p75v_25c_typical.lib"
elif lib == "OSC2M":
	path_lib = "/designs/cascade/ip/ig/OSC2M/rev1.1/lib/OSC2M_tt_0p75v_25c_typical.lib"
elif lib == "CAB":
	path_lib = "/designs/cascade/ip/ig/CAB/rev1.3.1/lib/CAB_tt_0p75v_25c_typical.lib"

path_lef = "/projects/analog_ip/workspace/xwqiu/cl/tacoma6/IP/"+lib+"/lef/"+name+".lef"




fp1 = FindPin(path_lef)
fp2 = FindPin(path_lib)

a = fp1.get_lef_pin()
b = fp2.get_lib_pin()
a_i = 0
b_i = 0

for i,line in enumerate(a):
	if "VDDi" in a[i][0]:
		a_i = i
		break

a = a[:a_i]+ a[a_i+1:]

for i,line in enumerate(b):
	if "VDDi" in b[i][0]:
		b_i = i
		break

b = b[:b_i]+ b[b_i+1:]

for i,line in enumerate(b):
	if a[i][0].lower() == b[i][0].lower() and a[i][1].lower() == b[i][1].lower():
		pass
	else:
		print(a[i], b[i])